﻿using AntDesign;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Web;
using SE.PO.CInterface.DBProtocols.Devices;
using SE.PO.CInterface.Po.Equips;

namespace SE.PO.CInterface.Web.Pages.DB.Components;
class SearchFormValue
{
    public string Value { get; set; }
}
public partial class SelectEquips
{
    #region query
    
    private Form<SearchFormValue> _form;
    
    private SearchFormValue _model = new SearchFormValue();
    
    List<EquipmentDto> _source = new();

    private string EquipName { get; set; } = "";
    
    public  IEnumerable<EquipmentDto> SelectedEquips;

    [Parameter] public string DeviceType{ get; set; }
    
    ITable? _table;

    int _pageIndex = 1;
    int _pageSize ;
    int _total;
    private bool _loadingData;

    protected override async Task OnInitializedAsync()
    {
        await GetSourceAsync(_pageIndex, _pageSize,"");
    }
    
    private  async Task  OnFinish(EditContext editContext)
    {
        await GetSourceAsync(_pageIndex, _pageSize);
    }

    private async Task Reset(MouseEventArgs args)
    {
        _form.Reset();
        await GetSourceAsync(_pageIndex, _pageSize,"");
    }

    public async Task GetSourceAsync(int pageIndex=0, int pageSize=int.MaxValue,string? deviceType="")
    {
        try
        {
            _loadingData = true;
            var result = await Client.Get(EquipName, deviceType);
            if (result != null)
            {
                _source = result.Items.ToList();
                _total = _source.Count;
                _pageSize = result.TotalCount;
            }
            _loadingData = false;
            StateHasChanged();
            _loadingData = false;
        }
        catch (System.Exception ex)
        {
            _ = Notice.Error(new()
            {
                Duration = 100,
                Message = "异常",
                Description = ex.Message
            });
        }
        finally
        {
            _loadingData = false;
        }
    }
    public async Task OnChange(QueryModel<EquipmentDto> queryModel)
    {
        // await GetSourceAsync(_pageIndex, _pageSize);
    }

    #endregion

}